﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using System.Linq;
using System.Collections.Generic;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ArrivalRegisterStorage1 : EFViewCmes
    {
        public ArrivalRegisterStorage1()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }
        
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (ValidateResultField())
            {
                if (!String.IsNullOrEmpty(this.fdInvoice.Value.ToString()) && !String.IsNullOrEmpty(this.fdInvoiceSum.Value.ToString()))
                {
                    this.fdSum.Value = this.fdInvoiceSum.GetDataValue();
                }

                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCMF3");
                eiInfo.Set("method", "insertMaterial");

                MaterialBlock(eiInfo);
                ResultBlock(eiInfo);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoWorkCompleted);
            }
        }

        /// <summary>
        /// 新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                CmesWindows _window = this.Parent as CmesWindows;
                _window.DialogResult = true;
                _window.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void fdImportType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (this.fdImportType.Value != null && "一般入库".Equals(this.fdImportType.Value.ToString()))
            {
                this.fdInvoice.eRegex = "^[a-zA-Z0-9]{1,50}$";
                this.fdInvoice.eValidateErrorPrompt = "输入1-50个字符，包括字母、数字";
                this.fdInvoice.eLabel = "*发票号";
                this.fdInvoice.Width = 220;
                this.fdInvoiceSum.eRegex = "^(-?\\d{1,10})(\\.\\d{0,5})?$";
                this.fdInvoiceSum.eLabel = "*发票金额";
                this.fdInvoiceSum.Width = 220;
                this.fdInvoice.IsReadOnly = false;
                this.fdInvoiceSum.IsReadOnly = false;
            }
            else if (this.fdImportType.Value != null && "估收入库".Equals(this.fdImportType.Value.ToString()))
            {
                this.fdInvoice.eRegex = "^[a-zA-Z0-9]{0,50}$";
                this.fdInvoice.eValidateErrorPrompt = "输入0-50个字符，包括字母、数字";
                this.fdInvoice.eLabel = "发票号";
                this.fdInvoice.Value = "";
                this.fdInvoice.IsReadOnly = true;
                this.fdInvoice.Width = 220;
                this.fdInvoiceSum.eRegex = "^(-?\\d{0,10})(\\.\\d{0,5})?$";
                this.fdInvoiceSum.eLabel = "发票金额";
                this.fdInvoiceSum.Width = 220;
                this.fdInvoiceSum.Value = "";
                this.fdInvoiceSum.IsReadOnly = true;
            }
        }

        private void Next_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateMaterialField())
            {
                if (this.material_fdNewMaterial.Value.Equals("1"))
                {
                    ValidateCode();
                }
                else
                {
                    SkipToNextPage();
                }
            }
        }

        private void ValidateCode()
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCMF3");
            eiInfo.Set("method", "validateCode");
            eiInfo.Set("fdMaterialCode", (this.material_fdMaterialCode.Value).ToString() + (this.material_fdMaterialLastCode.Value).ToString());

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoValidateCompleted);
        }

      /// <summary>
      /// 验证后的回调方法
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void OnDoValidateCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() != -1)
            {
                SkipToNextPage();
            }
            else
            {
                this.OnCommitMessagePopup(e.EIInfo);//操作失败，消息提醒
            }
        }

        private void SkipToNextPage()
        {
            this.FirstButtonPanel.Visibility = Visibility.Collapsed;
            this.MaterialBasicPanel.Visibility = Visibility.Collapsed;
            
            this.fdMaterialCode.Value = this.material_fdMaterialCode.Value;

            if ("1".Equals(this.material_fdNewMaterial.Value))
            {
                this.fdMaterialName.Value = this.material_fdMaterialName.Value;
            }
            else
            {
                this.fdMaterialName.Value = this.material_fdMaterialName1.Value;
            }
            this.fdMaterialType.Value = this.material_fdMaterialType.Value;
            this.fdMetric.Value = this.material_fdMetric.Value;
            this.fdSpecModel.Value = this.material_fdSpecModel.Value;
            this.fdNumber.Value = this.material_fdNumber.Value;
            this.fdQuality.Value = this.material_fdQuality.Value;
            this.fdPrice.Value = this.material_fdPrice.Value;

            this.eTitle = "到货登记管理—办理入库";
            this.InsertGrid.Visibility = Visibility.Visible;
            this.SecondButtonPanel.Visibility = Visibility.Visible;
        }

        private void BackClick(object sender, RoutedEventArgs e)
        {
            this.FirstButtonPanel.Visibility = Visibility.Visible;
            this.MaterialBasicPanel.Visibility = Visibility.Visible;

            this.eTitle = "到货登记管理—新增物资资料";
            this.InsertGrid.Visibility = Visibility.Collapsed;
            this.SecondButtonPanel.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// EFDatafield容器的全局合法性校验
        /// </summary>
        /// <param name="element">EFDatafield的容器控件</param>
        /// <returns>是否全部合法</returns>
        public bool ValidateMaterialField()
        {
            bool result = true;

            List<EFDataField> dataFields = this.GetChildObjects<EFDataField>(null);
            foreach (EFDataField field in dataFields)
            {
                if(field.eBindingName.Contains("material-0"))
                {
                    if (field.Visibility == Visibility.Visible)
                    {
                        result = field.Validate() && result;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// EFDatafield容器的全局合法性校验
        /// </summary>
        /// <param name="element">EFDatafield的容器控件</param>
        /// <returns>是否全部合法</returns>
        public bool ValidateResultField()
        {
            bool result = true;

            List<EFDataField> dataFields = this.GetChildObjects<EFDataField>(null);
            foreach (EFDataField field in dataFields)
            {
                if (field.eBindingName.Contains("result-0"))
                {
                    if (field.Visibility == Visibility.Visible)
                    {
                        result = field.Validate() && result;
                    }
                }
            }
            return result;
        }

        private void fdMaterialTypeId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    string fdMaterialTypeInnerCode = picker.ItemObject.GetType().GetProperty("fdTypeInnerCode").GetValue(picker.ItemObject, null) as string;
                    string fdTypeName = picker.ItemObject.GetType().GetProperty("fdTypeName").GetValue(picker.ItemObject, null) as string;
                    string fdTypeAllCode = picker.ItemObject.GetType().GetProperty("fdTypeAllCode").GetValue(picker.ItemObject, null) as string;

                    this.material_fdMaterialTypeInnerCode.Value = fdMaterialTypeInnerCode;
                    this.material_fdTypeCode.Value = fdTypeAllCode;
                    this.material_fdMaterialCode.Value = fdTypeAllCode;
                    this.material_fdMaterialType.Value = fdTypeName;
                }
                else
                {
                    return;
                }
            }         
        }

        /// <summary>
        /// 将单据信息时单据信息组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void MaterialBlock(EIInfo eiInfo)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.MaterialInsertGrid.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;

            EIBlock noteBlock = new EIBlock("material");
            noteBlock.SetColumns(base.GetDataFieldColumns("material"));
            noteBlock.AddRow(base.GetDataFieldValues("material"));
            eiInfo.AddBlock(noteBlock);
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void ResultBlock(EIInfo eiInfo)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertGrid.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
            EIBlock resultBlock = new EIBlock("result");
            resultBlock.SetColumns(base.GetDataFieldColumns("result"));
            resultBlock.AddRow(base.GetDataFieldValues("result"));
            eiInfo.AddBlock(resultBlock);
        }

        /// <summary>
        /// 是否新建物资
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdNewMaterial_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (this.material_fdNewMaterial.Value != null)
            {
                if ("1".Equals(this.material_fdNewMaterial.Value))
                {
                    this.material_fdMaterialName.Visibility = Visibility.Visible;
                    this.material_fdMaterialName1.Visibility = Visibility.Collapsed;
                    this.material_fdMaterialName.eBindingName = "material-0-fdMaterialName";
                    this.material_fdMaterialName1.eBindingName = "material-0-fdMaterialName1";
                    this.material_fdMaterialLastCode.IsReadOnly = false;
                    this.material_fdMaterialTypeId.IsEnabled = true;
                    this.material_fdMaterialTypeId.Value = "";
                    (this.material_fdMaterialTypeId.Content as CmesCustomView).Display = "";
                    this.material_fdPrice.IsReadOnly = false;
                }
                else
                {
                    this.material_fdMaterialName.Visibility = Visibility.Collapsed;
                    this.material_fdMaterialName1.Visibility = Visibility.Visible;
                    this.material_fdMaterialName.eBindingName = "material-0-fdMaterialName1";
                    this.material_fdMaterialName1.eBindingName = "material-0-fdMaterialName";
                    this.material_fdMaterialLastCode.IsReadOnly = true;
                    this.material_fdMaterialTypeId.IsEnabled = false;
                    this.material_fdPrice.IsReadOnly = true;
                }
            }
        }

        /// <summary>
        /// 选择物资带出其它信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void material_fdMaterialName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker == null)
            {
                return;
            }
            if (picker.ItemObject != null && !"".Equals(picker.ItemObject))
            {
                this.material_fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                this.material_fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null);
                this.material_fdMaterialInnerCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialInnerCode").GetValue(picker.ItemObject, null);
                this.material_fdMaterialLastCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialLastCode").GetValue(picker.ItemObject, null);
                this.material_fdMaterialType.Value = picker.ItemObject.GetType().GetProperty("fdMaterialType").GetValue(picker.ItemObject, null);
                this.material_fdMaterialTypeId.Value = picker.ItemObject.GetType().GetProperty("fdMaterialTypeId").GetValue(picker.ItemObject, null);
                (this.material_fdMaterialTypeId.Content as CmesCustomView).Display = picker.ItemObject.GetType().GetProperty("fdMaterialType").GetValue(picker.ItemObject, null); 
                this.material_fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null);
                this.material_fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null);
                this.material_fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null);
                this.material_fdPrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);
                this.material_fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null);
                
                this.material_fdMatchDevice.Value = picker.ItemObject.GetType().GetProperty("fdMatchDevice").GetValue(picker.ItemObject, null);
                this.material_fdPurchasePeriod.Value = picker.ItemObject.GetType().GetProperty("fdPurchasePeriod").GetValue(picker.ItemObject, null);
                this.material_fdWeight.Value = picker.ItemObject.GetType().GetProperty("fdWeight").GetValue(picker.ItemObject, null);
                this.material_fdSize.Value = picker.ItemObject.GetType().GetProperty("fdSize").GetValue(picker.ItemObject, null);
                this.material_fdParameter.Value = picker.ItemObject.GetType().GetProperty("fdParameter").GetValue(picker.ItemObject, null);
                this.material_fdFactory.Value = picker.ItemObject.GetType().GetProperty("fdFactory").GetValue(picker.ItemObject, null);
                this.material_fdSupplyName.Value = picker.ItemObject.GetType().GetProperty("fdSupplyName").GetValue(picker.ItemObject, null);
                this.material_fdMaterialStatus.Value = picker.ItemObject.GetType().GetProperty("fdMaterialStatus").GetValue(picker.ItemObject, null);
                this.material_fdRemark.Value = picker.ItemObject.GetType().GetProperty("fdRemark").GetValue(picker.ItemObject, null);               
            }
        }
    }
}
